<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;

class RequestHelper extends JooSamHelper{

	public function request($sid){

		$params = $this->Params();
		$settings = $this->config();
		$sid = (int)$sid;
		$songcheck = $this->checksong($sid);
		if($songcheck){
			$request = $this->makeRequest($sid);
			$code = (int)$request->code;
			$rid = (int)$request->rid;
		
			if(isset($settings->txt[$code])){
				$text = $settings->txt[$code];
			}else{
				$text = JText::sprintf('COM_JOOSAM_RESPONSE_TXT_UNKNOWN', $code, $request->url);
			}
		} else{
			$code = 503;
			$rid = 0;
			$text = $settings->txt->$code;
		}
		
		
		$result = (object) array(
							'code' => $code,
							'txt' => $text,
							'songid' => $sid,
							'msg' => $settings->request->message,
							'request' => $rid
						);
		return $result;
	}

	protected function checksong($sid){

		$params = $this->Params();
		$settings = $this->config();
		$req_cat = $settings->request->category_accapt;
		$req_type = $settings->request->songtype;
		$db = new samdbo();
		$query = "	SELECT S.ID, S.artist, S.title
					FROM songlist S, categorylist C
					WHERE S.ID = ".$sid."
					AND C.categoryID IN(".$req_cat.")
					AND S.songtype IN(".$req_type.")
					AND S.ID = C.songID ";
		$row = $db->getObjectList($query);
		if($row){
			if(is_array($row)){$row = $row[0];}
			if($row->ID == $sid){return true;}
			else{return false;}
		} else{ return false; }
	}
	
	protected function makeRequest($sid){

		$params = $this->Params();
		$settings = $this->config();
		$samip = $settings->sam->ip;
		$samport = $settings->sam->port;
		$clientip = urlencode(getenv('REMOTE_ADDR'));
		$songid = (int)$sid;

		$url = 'http://'.$samip.':'.$samport.'/req/?songID='.$songid.'&host='.$clientip;

		if(function_exists('curl_init')){
			$srv = curl_init($url);
			curl_setopt($srv, CURLOPT_HEADER, false);
			curl_setopt($srv, CURLOPT_RETURNTRANSFER, true);
			$data = curl_exec($srv);
			curl_close($srv);
			if($data == ''){
				$code = 500;
				$rid = 0;
			}
			else if($data == 'File not found'){
				$code = 501;
				$rid = 0;
			}
			else{
				$xml = simplexml_load_string($data);
				$code = (int)$xml->status->code;
				$rid = (int)$xml->status->requestID;
			}
		} else{
			$code = 502;
			$rid = 0;
		}
		
		// Output
		$result = (object) array(
							'code' => $code,
							'rid' => $rid,
							'url' => $url
					);
		return $result;
	}

	public function sendMessage(){
		
		$name = JRequest::getVar('jsm_clientname', 'Unknown');
		$message = JRequest::getVar('jsm_clientmsg', 'Unknown');
		$type = JRequest::getVar('jsm_msgtype', 0);
		$req_id = JRequest::getVar('jsm_reqid', 0);
		$data_request_name = str_replace('"', "'", $name);
		$data_request_msg = str_replace('"', "'", $message);

		if($type == 0){
			$result = 0;
		} else if($type == 2 and $name == 'Unknown'){
			$result = 1;
		} else{
			$db = new samdbo();
			$query = "	UPDATE requestlist
							SET name=\"$data_request_name\", msg=\"$data_request_msg\"
							WHERE ID=\"$req_id\" ; ";
			$row = $db->sendQuery($query);
			if($row){
				$updated = (int)$row;
				if($updated >= 0){
					$result = 2;
				} else{
					$result = 3;
				}
			} else{
				$result = 4;
			}
		}
		return $result;
	}

}
